package pl.vanta.utils.gui.tables.renderers;

import java.text.DecimalFormat;

import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

public class SizeRenderer extends DefaultTableCellRenderer
{
	private static final long	serialVersionUID	= 1;

	private SizeRendererParam	param;

	private DecimalFormat		format;

	public SizeRenderer(SizeRendererParam param)
	{
		this.param = param;

		this.format = new DecimalFormat();
	}

	public SizeRenderer(SizeRendererParam param, String pattern)
	{
		this.param = param;

		this.format = new DecimalFormat(pattern);
	}

	@Override
	public int getHorizontalAlignment()
	{
		return SwingConstants.RIGHT;
	}

	@Override
	protected void setValue(Object value)
	{
		if (value instanceof Number)
		{
			Number size = (Number) value;
			double sizeDouble = size.doubleValue() / this.param.getDivider();

			String sizeString = this.format.format(sizeDouble) + " " + this.param.getUnit();

			super.setValue(sizeString);
		}
		else
		{
			super.setValue(value);
		}
	}
}